<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterPermissionTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $tableNames = config('permission.table_names');

        if (empty($tableNames)) {
            throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
        }

        Schema::table($tableNames['permissions'], function (Blueprint $table) {
            $table->string('display_name')->default('')->comment('显示名称');
            $table->tinyInteger('is_menu')->default(0)->comment('是否菜单');
            $table->string('icon', 50)->default('')->comment('图标');
            $table->bigInteger('parent_id')->default(0)->comment('父节点');
            $table->integer('sort')->default(0)->comment('排序');
        });

        Schema::table($tableNames['roles'], function (Blueprint $table) {
            $table->string('display_name')->default('')->comment('显示名称');
            $table->tinyInteger('status')->default(1)->comment('状态');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $tableNames = config('permission.table_names');

        if (empty($tableNames)) {
            throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
        }

        Schema::table($tableNames['permissions'], function (Blueprint $table) {
            $table->dropColumn('display_name');
            $table->dropColumn('is_menu');
            $table->dropColumn('icon');
            $table->dropColumn('parent_id');
            $table->dropColumn('sort');
        });

        Schema::table($tableNames['roles'], function (Blueprint $table) {
            $table->dropColumn('display_name');
            $table->dropColumn('status');
        });
    }
}
